package com.itheima.controller;

import com.itheima.admin.setmealpojo.vo.SetmealVo;
import com.itheima.common.JsonResult;
import com.itheima.admin.AdminPage;
import com.itheima.service.SetmealService;
import com.itheima.admin.setmealpojo.dto.SetmealDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuzhiyong
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加
     *
     * @param setmealDto setmealDto
     * @return JsonResult
     */
    @PostMapping
    public JsonResult<String> setmealAdd(@RequestBody SetmealDto setmealDto) {
        return setmealService.addSetmeal(setmealDto) ? JsonResult.success("新增套餐成功", null) : JsonResult.error("新增套餐失败");
    }

    /**
     * 分页
     *
     * @param name     name
     * @param page     page
     * @param pagesize pagesize
     * @return JsonResult
     */
    @GetMapping(value = "/page")
    public JsonResult<AdminPage<SetmealVo>> setmealPage(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "page") Integer page, @RequestParam(value = "pageSize") Integer pagesize) {
        AdminPage<SetmealVo> adminPage = setmealService.getpageSetmeal(name, page, pagesize);
        return JsonResult.success(adminPage, null);
    }

    /**
     * 删除
     *
     * @param ids ids
     * @return JsonResult
     */
    @DeleteMapping
    public JsonResult<String> setmealRemove(@RequestParam(value = "ids") String[] ids) {
        return setmealService.removeByIdsSetmeal(ids) ? JsonResult.success("删除成功", null) : JsonResult.error("该套餐正在发售，不能删除");
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return JsonResult
     */
    @GetMapping(value = "/{id}")
    public JsonResult<SetmealDto> setmealId(@PathVariable(value = "id") String id) {
        SetmealDto setmealDto = setmealService.getBySetmealId(id);
        if (setmealDto != null) {
            return JsonResult.success(setmealDto, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 修改
     *
     * @param setmealDto setmealDto
     * @return JsonResult
     */
    @PutMapping
    public JsonResult<String> setmealUpdate(@RequestBody SetmealDto setmealDto) {
        return setmealService.updateSetmeal(setmealDto) ? JsonResult.success("修改成功", null) : JsonResult.error("修改失败");
    }

    /**
     * 批量启停用
     *
     * @param status status
     * @param ids    ids
     * @return JsonResult
     */
    @PostMapping(value = "/status/{status}")
    public JsonResult<String> setmealStatus(@PathVariable(value = "status", required = false) Integer status, @RequestParam(value = "ids") String[] ids) {
        return setmealService.modifyBystatus(status, ids) ? JsonResult.success("操作成功", null) : JsonResult.error("操作失败");
    }
}
